package com.resou.reader.bookstore.category;

import android.arch.lifecycle.LifecycleOwner;
import android.support.annotation.NonNull;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.SortDataBean;
import com.resou.reader.api.entry.SortSubBean;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.base.m.IModel;
import com.resou.reader.base.p.BasePresenter;
import com.resou.reader.bookstore.category.view.IAllCategoryView;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.log.DLog;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ${Garrett} on 2018/9/17.
 * Contact me krouky@outlook.com
 */
public class AllCategoryPresenter extends ResouBasePresenter<IAllCategoryView> {
    private StoreService mService;
    private List<SortDataBean<List<SortSubBean>>> data;

    public AllCategoryPresenter(IAllCategoryView view) {
        super(view);
        mService = ApiImp.getInstance().getService(StoreService.class);
    }

    public void quearData() {
        mService.appCategory().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<SortDataBean<List<SortSubBean>>>>>() {
                    @Override
                    public void accept(Result<List<SortDataBean<List<SortSubBean>>>> listResult) throws Exception {
                        data = listResult.getData();
                        mView.refresh(data);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showError(throwable);
                    }
                });
    }

    public void startToSubCategory(int index, int position) {
        SortSubBean sortSubBean = data.get(index).getSubSort().get(position);
        SubCategoryActivity.startActivity(mView.getViewContext(), sortSubBean);
    }
}
